<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\facade\WeChat;
use app\common\facade\WeChatMini;
use app\common\model\Area;
use app\common\model\Carousel;
use app\common\model\Config;
use app\common\model\Curriculum;
use app\common\model\CurriculumCate;
use app\common\model\Documentation;
use app\common\model\HotCities;
use app\common\model\News;
use app\common\model\Order;
use app\common\model\ProductCate;
use app\common\model\Product;
use app\common\model\Cooperative;
use app\common\model\User as UserModel;
use app\common\tool\Result;
use think\facade\Cache;
use think\facade\View;

class Index extends Base
{
    /**
     * get: 协议
     * path: communal
     * method: communal
     * param: type {string} register_msg注册协议  company公司介绍  buy_msg购买流程
     */
    public function communal()
    {
        $type = $this->request->param('type');
        if($type==''){
            return Result::fail('获取内容失败！');
        }
        $title = ['company' => '公司介绍', 'register_msg' => '注册协议','buy_msg'=>'购买流程'];
        $content=\app\common\controller\Config::lists()[$type];
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
        $content = htmlspecialchars_decode(str_replace('/uploads/', $url, $content));
        $content = str_replace('\\', '', $content);
        $data=[
            'title' => $title[$type],
            'content' => $content
        ];
        return Result::success('获取数据成功！',$data);
    }


    public function payment_open()
    {
        $payment_open = cache('system')['payment_open'];
        return Result::success('获取数据成功！',$payment_open);
    }

    public function get_openid()
    {
        $post = $this->request->post();
        $result = WeChatMini::session($post['code']);
        if($result['openid'])
        {
            User::update(['openid'=>$result['openid']],['id'=>$this->user->id]);
            return Result::success('获取数据成功！');
        }else{
            return Result::fail('获取数据成功！');
        }
    }

    /**
     * get: 轮播图
     * path: carousel
     * method: carousel
     */
    public function carousel(): \think\Response
    {
        $data['carousel'] = Carousel::getList(['type' => 1]);
        return Result::success('获取成功', $data);

    }


    /**
     * get:  新闻轮播
     * path: news_reveal
     * method: news_reveal
     */
    public function news_reveal(): \think\Response
    {
        $news = News::where(['is_del'=>0,'status'=>1,'type'=>1])->order('sort','asc')->order('create_time','desc')->field('id,title')->select();
        return Result::success('获取成功', $news);

    }


    /**
     * get: 推荐商品
     * path: recommend
     * method: recommend
     */
    public function recommend(): \think\Response
    {
        $documentation = Product::where(['is_del'=>0,'status'=>1,'pick'=>1])->order('sort','asc')->order('create_time','desc')->limit(3)->field('id,title,price,vip_price,icon')->select();
        return Result::success('获取成功', $documentation);

    }


    /**
     * get: 课程分类
     * path: getCurriculumCate
     * method: getCurriculumCate
     */
    public function getCurriculumCate(): \think\Response
    {
        $documentation = CurriculumCate::where(['is_del'=>0,'status'=>1,'pid'=>0])->order('sort','asc')->field('id,title,icon')->select();
        return Result::success('获取成功', $documentation);
    }

    /**
     * get: 首页课程
     * path: getCurriculum
     * method: getCurriculum
     * param: cate
     */
    public function getCurriculum(): \think\Response
    {
        $param = $this->request->param();
        $documentation = Curriculum::where(['is_del'=>0,'status'=>1,'cate_1'=>$param['cate']])->order('sort','asc')->order('create_time','desc')->limit(2)->field('id,title,price,introduction,images,frequency')->select();
        return Result::success('获取成功', $documentation);
    }




}
